package com.hdl.service.impl;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSON;
import com.hdl.dao.ArticleDAO;
import com.hdl.dto.ArticleSearch;
import com.hdl.entity.Article;
import com.hdl.exception.CodeException;
import com.hdl.service.ArticleService;
import com.hdl.vo.ArticleVO;
import com.hdl.vo.PageVO;

import cn.hutool.core.util.StrUtil;

public class ArticleServiceImpl implements ArticleService {

	private static Logger log = LoggerFactory.getLogger(ArticleServiceImpl.class);
	private ArticleDAO articleDAO = new ArticleDAO();
	
	@Override
	public long countBySearch(ArticleSearch search) throws CodeException {
		
		return 0;
	}

	@Override
	public PageVO<ArticleVO> selectSearch(ArticleSearch search,Integer current, Integer rowMax) throws CodeException {
		try {
			long total = articleDAO.countBySearch(search);
			PageVO<ArticleVO> page = new PageVO<>(total, current, rowMax);
			return page.setList(articleDAO.selectSearch(search, page.getOffset(), page.getRowMax()));
		} catch (SQLException e) {
			log.warn("获取文章分页数据失败",e);
			throw new CodeException(551, "获取文章分页数据失败",e);
		}
	}

	@Override
	public int add(Article article) throws CodeException {
		try {
			
			
			//提取文章正文中的前3张图片
			List<String> imgs = new ArrayList<>();
			Pattern pattern = Pattern.compile("<img[^>]+src=\"([^\"]+)\"[^>]+>");
			Matcher matcher = pattern.matcher(article.getArticleContent());
			int count=0;
			while(matcher.find()&&count<3){
				imgs.add(matcher.group(1));
				count++;
			}
			article.setArticleImgs(JSON.toJSONString(imgs));
			
			
			article.setDeleted(false);
			article.setArticleStatus(1);
			article.setArticleTime(new Date());
			
			return articleDAO.add(article);
		} catch (SQLException e) {
			log.warn("添加文章错误",e);
			throw new CodeException(552, "添加文章错误",e);
		}
	}

	@Override
	public int update(Article article) throws CodeException {
		try {
			//提取文章正文中的前3张图片
			List<String> imgs = new ArrayList<>();
			Pattern pattern = Pattern.compile("<img[^>]+src=\"([^\"]+)\"[^>]+>");
			Matcher matcher = pattern.matcher(article.getArticleContent());
			int count=0;
			while(matcher.find()&&count<3){
				imgs.add(matcher.group(1));
				count++;
			}
			article.setArticleImgs(JSON.toJSONString(imgs));
			
			return articleDAO.add(article);
		} catch (SQLException e) {
			log.warn("修改文章错误",e);
			throw new CodeException(553, "修改文章错误",e);
		}
	}

	@Override
	public Article selectById(Long id) throws CodeException {
		try {
			return articleDAO.selectById(id);
		} catch (SQLException e) {
			log.warn("查看文章错误",e);
			throw new CodeException(554, "查看文章失败",e);
		}
	}

	@Override
	public int deleteById(Long id) throws CodeException {
		try {
			return articleDAO.deleteById(id);
		} catch (SQLException e) {
			log.warn("删除文章失败",e);
			throw new CodeException(554, "删除文章失败",e);
		}
	}

	@Override
	public Article selectPrevious(Long articleId) throws CodeException {
		try {
			return articleDAO.selectPrevious(articleId);
		} catch (SQLException e) {
			log.warn("查看上一篇文章失败",e);
			throw new CodeException(555, "查看上一篇文章失败",e);
		}
	}

	@Override
	public Article selectNext(Long articleId) throws CodeException {
		try {
			return articleDAO.selectNext(articleId);
		} catch (SQLException e) {
			log.warn("查看下一篇文章失败",e);
			throw new CodeException(556, "查看下一篇文章失败",e);
		}
	}

}
